const schemaJoi = require('./util/schemaJoi.js');
const Joi = require('joi');
const service = require('./service/service.js');
const CryptoJS = require("crypto-js");



//初始实例化
exports.initV = async (context) => {
	// console.log("--------------addCartOder-------------",CryptoJS.enc.Base64.stringify(CryptoJS.HmacSHA256(signSecret, signSecret)))
	const {cloud,mixNick, userNick, main_openid,mainUserOpenId ,data:{icon ,name,template_id="3000000026610802",template_version,description}} = context
  // console.log("用户主id",mainUserOpenId);
  context.data = {
    icon,
    name,
    template_id,
    template_version,
    description,
    mainUserOpenId
}
	try {
		const schema = Joi.object().keys({
      icon: Joi.string(),
			name: Joi.string().required(),
			template_id: Joi.string().required(),
			template_version: Joi.string().required(),
      description: Joi.string().required(),
      mainUserOpenId: Joi.string().required(),
    });
    await schemaJoi(schema,context.data);
		return await service.initV(context);
	} catch (e) {
    // console.log(JSON.stringify(e));
		return { errCode: 1111, msg: '铺捉到错误' + e };
	}
};



//查询列表

exports.getVersion = async (context) => {
	const {cloud,mixNick, userNick, main_openid,mainUserOpenId ,data} = context

	try {
    return await service.getVersion(context,mainUserOpenId);

	} catch (e) {
		return { errCode: 1111, msg: '铺捉到错误' + e };
	}
};

//获取当前线上版本状态
exports.getonline_Version = async (context) => {
	const {cloud,mixNick, userNick, main_openid,mainUserOpenId ,data} = context
  console.log(mainUserOpenId);
	try {
    return await service.getonline_Version(context,mainUserOpenId);

	} catch (e) {
		return { errCode: 1111, msg: '铺捉到错误' + e };
	}
};


//更新版本
exports.update_Version = async (context) => {
	const {cloud,mixNick, userNick, main_openid,mainUserOpenId ,data:{app_id="1",template_id="123",template_version="0.0.2"}} = context
  context.data = {
    app_id,
    template_id,
    template_version
  }

	try {
    	const schema = Joi.object().keys({
			app_id: Joi.string().required(),
			template_id: Joi.string().required(),
			template_version: Joi.string().required(),
    });

    await schemaJoi(schema,context.data);
    return await service.update_Version(context);

	} catch (e) {
		return { errCode: 1111, msg: '铺捉到错误' + e };
	}
};


//上传版本
exports.Upload_Version = async (context) => {
	const {cloud,mixNick, userNick, main_openid,mainUserOpenId ,data:{app_id="1",template_id="123",template_version="0.0.2",app_version="0.0.9"}} = context
  context.data = {
    app_id,
    template_id,
    template_version,
    app_version
  }

	try {
    	const schema = Joi.object().keys({
			app_id: Joi.string().required(),
			template_id: Joi.string().required(),
			template_version: Joi.string().required(),
      app_version: Joi.string().required(),
    });

    await schemaJoi(schema,context.data);
    return await service.Upload_Version(context);


	} catch (e) {
		return { errCode: 1111, msg: '铺捉到错误' + e };
	}
};

















/*
* 添加埋点信息 
* @return data: object 埋点的数据
* */
exports.addUserStat = async (context) => {
		const {cloud, data, mixNick, userNick } = context
		console.log("============================",data)
  try {
    //参数验证
    const schema = Joi.object().keys({
      stat_data: Joi.object().required(),
    });
    await schemaJoi(schema, data);
    let paramsData = data.stat_data;
    paramsData.userId = mixNick;
    return await service.addUserStat(JSON.stringify(paramsData),cloud);
  } catch (e) {
    return {isOk:0,msg:'铺捉到错误'+e};
  }
};



